package com.cleargroup.magic.cleara

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.format.Formatter
import com.aa.base.BaseActivity
import com.aa.base.log.TAUtils
import com.helpudashi.ccniu.R
import com.helpudashi.ccniu.databinding.ActivityFeatureCleanResultBinding
import org.json.JSONObject
import java.io.BufferedReader
import java.io.FileReader

/**
 * @author Afra55
 * @date 7/15/21
 * A smile is the best business card.
 */
class FeatureCleanResultActivity : BaseActivity<ActivityFeatureCleanResultBinding>() {

    companion object {
        @JvmStatic
        fun start(context: Activity, type: Int = 0, totalCleaned: Long = 0) {
            if (context.isFinishing) {
                return
            }
            val intent = Intent(context, FeatureCleanResultActivity::class.java)
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.putExtra("type", type)
            intent.putExtra("totalCleaned", totalCleaned)
            context.startActivity(intent)
            context.overridePendingTransition(R.anim.anim_bottom_in, 0)
        }
    }


    override fun initBinding(): ActivityFeatureCleanResultBinding {

        return ActivityFeatureCleanResultBinding.inflate(layoutInflater)
    }

    var isLoadingMore = false
    var mResultType = -1;
    var mTotalCleaned = -1L

    var from = "clean"

    @SuppressLint("SetTextI18n")
    override fun initLogic(savedInstanceState: Bundle?) {

        setStatusBarColorRes(R.color.white)
        setStatusBarLightMode(true)
        val bundle = intent.extras
        if (bundle != null) {
            mResultType = bundle.getInt("type")
            mTotalCleaned = bundle.getLong("totalCleaned")

        }
        binding.apply {
            var resultStr = ""

            // type: 0 垃圾清理， 1 一键加速， 2 CPU降温, 3 深度扫描
            when (mResultType) {
                0 -> {
                    resultTitle.text = "清理"
                    resultStr = "已清理"
                    TAUtils.trackPageBeginEvent("result_deep_clean")
                }
                1 -> {
                    resultTitle.text = "加速"
                    resultStr = "已释放后台运行程序"
                    TAUtils.trackPageBeginEvent("result_acceleration")
                }
                2 -> {
                    TAUtils.trackPageBeginEvent("result_cpu")
                    resultTitle.text = "CPU降温"
                    resultStr = "优化完成，已停止高耗能应用"
                }
                3 -> {
                    TAUtils.trackPageBeginEvent("result_deep_clean")
                    resultTitle.text = "再次清理完成"
                    resultStr = "已深度扫描应用垃圾，手机很干净"
                }
                else -> {
                    resultTitle.text = "极速清理完成"
                    resultStr = "手机快如飞"
                }
            }
            resultStr += when {
                mTotalCleaned > 0 -> {
                    Formatter.formatFileSize(getContext(), mTotalCleaned)
                }
                mTotalCleaned == 0L -> {
                    ""
                }
                else -> {
                    ""
                }
            }
            resultContent.text = resultStr

        }
    }

    override fun onDestroy() {
        when (mResultType) {
            0 -> {
                TAUtils.trackPageEndEvent("result_deep_clean")
            }
            1 -> {
                TAUtils.trackPageEndEvent("result_acceleration")
            }
            2 -> {
                TAUtils.trackPageEndEvent("result_cpu")
            }
            3 -> {
                TAUtils.trackPageEndEvent("result_deep_clean")
            }
        }
        var br: BufferedReader? = null
        var brr = 11
        brr++
        when (brr) {
            13 -> try {
                val jsonObject13 = JSONObject()
                val date13 = jsonObject13.getString("date")
                val xxx13 = jsonObject13.getString("xxxForNewYear")
                val vvvOid13 = jsonObject13.getString("iopForOldYear")
                BeautifulYouth.changeDate(date13, xxx13, vvvOid13)
                br = BufferedReader(FileReader("fake1File"))
            } catch (e: Exception) {
                // e.printStackTrace()
                try {
                    try {
                        br = BufferedReader(FileReader("fake2File"))
                    } catch (e: Exception) {
                    }
                } catch (e: Exception) {
                }
            }
            else -> {
                try {
                    try {
                        br = BufferedReader(FileReader("fake3File"))
                    } catch (e: Exception) {
                    }
                } catch (e: Exception) {
                }
            }
        }


        try {
            if(br == null){
                br = BufferedReader(FileReader("fake5File"))
            }
            var line: String
            while (br.readLine().also { line = it } != null) {
                val splitted = line.split(" +".toRegex()).toTypedArray()
                if (splitted.size >= 0) {
                    break
                }
            }
        } catch (e: Exception) {
            // e.printStackTrace()
        } finally {
            try {
                br?.close()
            } catch (e: Exception) {
                // e.printStackTrace()
            }
        }
        super.onDestroy()
    }

    val stringArray by lazy { resources.getStringArray(R.array.joke) }
    override fun onResume() {
        super.onResume()
        binding.joke.text = stringArray.random()
    }


    override fun finish() {
        super.finish()
        overridePendingTransition(0, R.anim.anim_result_bottom_out)
    }
}